/**
 * author:Administrator
 * date:2016/3/15
 * desc: vendor command 接口
 */

'use strict';

let service = {};
module.exports = service;

let Vendor = require('../domain/model_cmd/vendor/Vendor.js');
let VendorRepo = require('../domain/model_cmd/vendor/VendorRepo.js');
let Category = require('../domain/model_cmd/category/Category.js');
let CateRepo = require('../domain/model_cmd/category/CategoryRepo.js');
let Location = require('../domain/model_cmd/location/Location.js');
let LocationRepo = require('../domain/model_cmd/location/LocationRepo.js');
let Shop = require('../domain/model_cmd/shop/Shop.js');
let ShopRepo = require('../domain/model_cmd/shop/ShopRepo.js');
let Ware = require('../domain/model_cmd/ware/Ware.js');
let WareRepo = require('../domain/model_cmd/ware/WareRepo.js');
let ShopWare = require('../domain/model_cmd/shopWare/ShopWare.js');
let ShopWareRepo = require('../domain/model_cmd/shopWare/ShopWareRepo.js');


/**
 * 创建一个新产品
 */
service.createWare = function*(vendorId, name, categoryId, total, price) {
  let vendor = yield VendorRepo.find(vendorId);
  if (!vendor) return false;

  let category = yield CateRepo.find(categoryId);
  if (!category) return false;

  if (total < 0) total = Ware.WARE_CONST.unlimit_count;
  let ware = Ware.freshNew({
    name: name,
    categoryId: categoryId,
    total: total,
    maxCount: total,
    price: price,
    vendorId: vendorId
  });
  yield ware.store();
  // 创建商品时，需要为商品创建快照
  yield ware.createSnapshot();
  return ware;
};

/**
 * 创建一个商铺
 */
service.createShop = function*(vendorId, name, locationId) {
  let vendor = yield VendorRepo.find(vendorId);
  if (!vendor) return false;

  let location = yield LocationRepo.find(locationId);
  if (!location) return false;

  let shop = new Shop({
    name: name,
    locationId: locationId,
    vendorId: vendorId
  });
  yield shop.store();
  return shop;
};

/**
 * 将一个商品，上架到一个(或多个)商铺
 * @param vendorId
 * @param wareId
 * @param shopId
 * @returns {*}
 */
service.wareOnShelf = function *(vendorId, wareId, shopId) {
  let ware = yield WareRepo.find(wareId);
  if (!ware) return false;
  if (vendorId != ware.vendorId) return false;

  if (Array.isArray(shopId)) {
    let list = [];
    for (let i = 0; i < shopId.length; i++) {
      let res = yield _singleShopWare(vendorId, shopId[i], ware);
      if (res) list.push(res);
    }
    return list;
  } else {
    return yield _singleShopWare(vendorId, shopId, ware);
  }

};
/**
 * 添加商品到一个shop
 * @param sid
 * @returns {*}
 */
function * _singleShopWare(vendorId, sid, ware) {
  let shop = yield ShopRepo.find(sid);
  if (!shop) return false;
  if (vendorId != shop.vendorId) return false;

  // 该 ware 与 shop 已经关联
  let preObj = yield ShopWareRepo.findByShopIdWareId(sid, ware.id);
  if (preObj) return;

  let shopWare = new ShopWare({
    shopId: sid,
    wareId: ware.id,
    vendorId: shop.vendorId,
    categoryId: ware.categoryId,
    locationId: shop.locationId
  });
  yield ShopWareRepo.store(shopWare);
  return shopWare;
}


/**
 * 修改 ware
 */
service.updateWare = function * (vendorId, wareId, updatedAttr) {
  let vendor = yield VendorRepo.find(vendorId);
  if (!vendor) return false;

  let warePojo = yield WareRepo.find(wareId);
  if (!warePojo) return false;
  if (warePojo.vendorId != vendorId) return false;

  // 升级
  let ware = Ware.rebuild(warePojo);
  yield ware.updateWare(updatedAttr);

  return ware;
};

let CouponMjs = require('../domain/model_cmd/coupon/CouponMjs.js');
let CouponRepo = require('../domain/model_cmd/coupon/CouponRepo.js');

/**
 * 普通商家创建一个 满就送 coupon
 * @param vendorId
 * @param applyDeadline 申请截止日期 null
 * @param useDeadline 使用截止日期 null
 * @param maxCount 最大申请次数 null
 * @param consumeLimit 最低消费额度时可用
 * @param money 减免金额
 * @param limitWareId 限定特定商品
 * @returns {*}
 */
service.createCouponMjs = function * (vendorId, applyDeadline, useDeadline, maxCount, consumeLimit, money, limitWareId) {
  //console.log('createCouponMjs');
  let vendor = yield VendorRepo.find(vendorId);
  //console.log('vendor=', vendor);
  if (!vendor) return false;
  if (maxCount == null || maxCount < 0) maxCount = CouponMjs.COUPON_CONST.unlimit_count;
  let coupon = CouponMjs.freshNew({
    applyDeadline: applyDeadline,
    useDeadline: useDeadline,
    maxCount: maxCount,
    consumeLimit: consumeLimit,
    money: money,
    limitVendorId: vendorId,
    limitWareId: limitWareId
  });

  yield coupon.store();
  //console.log('vendorCmdService::createCouponMjs coupon=', coupon);
  return coupon;
};


let authService = require('../infrastructure/service/authService.js');
let ROLE_ENUM = authService.ROLE_ENUM;
let STATUS_ENUM = authService.STATUS_ENUM;
let Shopkeeper = require('../domain/model_cmd/shopkeeper/Shopkeeper.js');
let ShopkeeperRepo = require('../domain/model_cmd/shopkeeper/ShopkeeperRepo.js');

/**
 * 创建一个 shopkeeper
 * @param vendorId
 * @param name
 * @param pwd
 * @returns {*}
 */
service.createKeeper = function*(vendorId, name, pwd) {
  let user = yield authService.register(name, pwd, ROLE_ENUM.shopkeeper);
  if (!user) return false;
  let keeper = new Shopkeeper({uId: user.id, name: name, vendorId: vendorId});
  yield ShopkeeperRepo.store(keeper);
  return keeper;
};

/**
 * 向 shopkeeper 添加一个商店
 * @param vendorId
 * @param keeperId
 * @param shopId
 */
service.shopSetKeeper = function*(vendorId, keeperId, shopId) {
  let vendor = yield VendorRepo.find(vendorId);
  if (!vendor) return false;

  let keeper = yield ShopkeeperRepo.find(keeperId);
  if (!keeper) return false;
  if (keeper.vendorId != vendorId) return false;

  let shop = yield ShopRepo.find(shopId);
  if (!shop) return false;
  if (shop.vendorId != vendorId) return false;

  //
  shop = new Shop(shop);
  yield shop.setKeeper(keeper.id);
  //yield keeper.addShop(shopId);
  //return true;
  return shop;
};